﻿Public Class frmTaxCodeDE
    Dim validator As PropertyValidator = New PropertyValidator
    'Dim sqlManager As New SQLManager
    'Dim sqlList As New SQLList
    Dim payrollManager As New PayrollManager
    Private Sub frmTaxCodeDE_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        validator.ResetControls(grpTaxCode)

        If Clipboard.GetData(DataFormats.Text) <> "" Then
            Try
                'Dim taxcodeList As List(Of TaxCode) = sqlList.getItemTaxCode(Clipboard.GetData(DataFormats.Text))
                Dim taxcodeList As List(Of TaxCode) = payrollManager.getItemTaxCode(Clipboard.GetData(DataFormats.Text))
                For Each taxcode As TaxCode In taxcodeList
                    txtTaxCode.Text = taxcode.tax_code
                    txtDescription.Text = taxcode.tax_code_desc
                    txtExcemption.Text = taxcode.exemption
                Next
                btnSave.Text = "&Update"
                btnClear.Text = "&Cancel"
            Catch ex As Exception
                MsgBox("Can't load Web page" & vbCrLf & ex.Message)
            End Try
        End If
    End Sub
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If validator.IsEmptyString(txtTaxCode, "Tax Code") Then Return
        If validator.IsEmptyString(txtDescription, "Description") Then Return
        If validator.IsNumericString(txtExcemption, "Excemption") Then Return

        Try
            If Clipboard.GetData(DataFormats.Text) = "" Then
                payrollManager.insertTaxCode(txtTaxCode.Text, txtDescription.Text, txtExcemption.Text)
                'sqlManager.executeQuery("INSERT INTO ref_tax_code (tax_code, tax_code_desc, exemption) VALUE ('" + txtTaxCode.Text + "','" + txtDescription.Text + "'," + txtExcemption.Text + ")")
                MsgBox("Successfully saved.")
            Else
                payrollManager.updateItemTaxCode(txtTaxCode.Text, txtDescription.Text, txtExcemption.Text, Clipboard.GetData(DataFormats.Text))
                'SQLManager.executeQuery("UPDATE ref_tax_code SET tax_code = '" + txtTaxCode.Text + "', tax_code_desc='" + txtDescription.Text + "', exemption=" + txtExcemption.Text + " WHERE id=" + Clipboard.GetData(DataFormats.Text) + " ")
                MsgBox("Successfully updated.")
            End If
            ClearAllFields()
        Catch ex As Exception
            MsgBox("Can't load Web page" & vbCrLf & ex.Message)
        End Try
        Me.Close()
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        If Clipboard.GetData(DataFormats.Text) = "" Then
            validator.ResetControls(grpTaxCode)
        Else
            Me.Close()
        End If
    End Sub
    Public Function ClearAllFields()
        validator.ResetControls(grpTaxCode)
        btnSave.Text = "&Save"
        btnClear.Text = "&Clear"
        Return 0
    End Function
End Class